﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors.Controls;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Aksesuar
{
    public partial class AksesuarRezerveCoz : XForm
    {
        public AksesuarRezerveCoz()
        {
            InitializeComponent();
        }

        int AksesuarRezerveLinesId, AksesuarId;
        public AksesuarRezerveCoz(int AksesuarRezerveLinesId)
            : this()
        {
            this.AksesuarRezerveLinesId = AksesuarRezerveLinesId;
        }

        private void AksesuarRezerveCoz_Load(object sender, EventArgs e)
        {
            DataTable dt = ExecuteDataTable("GetAksesuarByRezervelinesId", AksesuarRezerveLinesId);
            DataRow dr = dt.Rows[0];

            if (dr["Photo"] != DBNull.Value)
            {
                MemoryStream ms = new MemoryStream((byte[])dr["Photo"]);
                Image pimg = Image.FromStream(ms);
                img.Image = pimg;
                img.Properties.SizeMode = PictureSizeMode.Stretch;
            }

            lblStokKod.Text = dr["StokKod"].ToString();
            lblMarka.Text = dr["Marka"].ToString();
            lblKategori.Text = dr["Kategori"].ToString();
            lblRenk.Text = dr["Renk"].ToString();
            lblEtiket.Text = dr["Etiket"].ToString();
            lblBoy.Text = dr["Boy"].ToString();
            lblAciklama.Text = dr["Aciklama"].ToString();
            lblCurrentQty.Text = dr["Qty"].ToString();
            txtQty.Value = dr["Qty"].ToDecimal();

            AksesuarId = dr["AksesuarId"].ToInt();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            decimal CurrentQty = lblCurrentQty.Text.ToDecimal();
            decimal RequestQty = txtQty.Value.ToDecimal();

            if (CurrentQty<RequestQty)
            {
                ShowErrorMessage("Çözülecek miktar rezerve miktarından fazla olamaz !");
                return;
            }

            if (RequestQty <= 0)
            {
                ShowErrorMessage("Çözülecek miktar 0'dan büyük olmalıdır !");
                return;
            }

            ExecuteNonQuery("UpdAksesuarRezerveLines", AksesuarRezerveLinesId, RequestQty, WhoAmI);
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
